package com.jfunit.java.util;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropsUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class);
    private static Properties props=null;
    /**
     * 加载属性文件
     * 
     * @param fileName
     * @return
     */
    public static Properties loadProps(String fileName) {
        if (props==null) {
            InputStream is = null;
            try {
                //System.out.println("classPath:"+Thread.currentThread().getContextClassLoader().getResource(""));
                is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
                if (is == null) {
                    throw new FileNotFoundException(fileName + "文件未找到");
                }            
                
                /*
                System.out.println("is:"+is);
                int bytesRead = 0;
                byte[] buffer = new byte[1024];
                //从文件中按字节读取内容，到文件尾部时read方法将返回-1
                while ((bytesRead = is.read(buffer)) != -1) {
                    //将读取的字节转为字符串对象
                    String chunk = new String(buffer, 0, bytesRead);
                    System.out.print(chunk);
                }*/

                
                props = new Properties();
                props.load(is);
            } catch (Exception e) {
                LOGGER.error("load properties failure", e);
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (Exception e) {
                        LOGGER.error("close input stream failure", e);
                    }
                }
            }
        }        
        return props;
    }

    /**
     * 获取字符型 属性值
     * 
     * @param configProps
     * @param jdbcDriver
     * @return
     */
    public static String getString(Properties props, String key) {
        return getString(props, key, "");
    }

    public static String getString(Properties props, String key, String defaultValue) {
        String value = defaultValue;
        if (props.containsKey(key)) {            
            value = props.getProperty(key);
        }
        return value;
    }

    public static int getInt(Properties props, String key) {
        return getInt(props, key, 0);
    }

    public static int getInt(Properties props, String key, int defautValue) {
        int value = defautValue;
        if (props.containsKey(key)) {
            value = CastUtil.castInt(props.getProperty(key));
        }
        return value;
    }

    public static long getLong(Properties props, String key) {
        return getLong(props, key, 0);
    }

    public static long getLong(Properties props, String key, long defautValue) {
        long value = defautValue;
        if (props.containsKey(key)) {
            value = CastUtil.castLong(props.getProperty(key));
        }
        return value;
    }

    public static double getDouble(Properties props, String key) {
        return getDouble(props, key, 0);
    }

    public static double getDouble(Properties props, String key, double defautValue) {
        double value = defautValue;
        if (props.containsKey(key)) {
            value = CastUtil.castDouble(props.getProperty(key));
        }
        return value;
    }

    public static boolean getBoolean(Properties props, String key) {
        return getBoolean(props, key, false);
    }

    public static boolean getBoolean(Properties props, String key, boolean defautValue) {
        boolean value = defautValue;
        if (props.containsKey(key)) {
            value = CastUtil.castBoolean(props.getProperty(key));
        }
        return value;
    }
}
